package common.ComboboxCheckboxTree
{ 
	import mx.controls.ComboBox;
	import mx.core.ClassFactory;
	import mx.events.DropdownEvent;
	import mx.events.ListEvent; 
	
	[Event(name="treeChange", type="mx.events.ListEvent")] 
	
	public class TreeComboBox extends ComboBox 
	{ 
		private var _ddFactory:ClassFactory; 
		
		/**
		 * 设置项目渲染器 
		 */		
		private function get ddFactory():ClassFactory 
		{ 
			if (_ddFactory == null) 
			{ 
				_ddFactory = new ClassFactory(); 
				_ddFactory.generator = TreeComboBoxRenderer; 
				_ddFactory.properties = { 
					width:this.width,  
					height:this._treeHeight, 
					outerDocument:this 
				}; 
			} 
			return _ddFactory;       
		}    
		
		/**
		 * 下拉树的高度 
		 */		
		private var _treeHeight:Number; 
		public function get treeHeight():Number 
		{ 
			return _treeHeight; 
		} 
		public function set treeHeight(value:Number):void 
		{ 
			this._treeHeight = value; 
			ddFactory.properties["height"] = this._treeHeight; 
		}
		
		/**
		 * 设定是否可以选择父级结点 
		 */		
		private var _isParentSelectable:Boolean = true; 
		[Inspectable(enumeration="true,false", defaultValue="true")] 
		public function set isParentSelectable(value:Boolean):void 
		{ 
			_isParentSelectable = value; 
		}
		public function get isParentSelectable():Boolean
		{
			return _isParentSelectable;
		}
		
		/**
		 * 选中项 
		 */		
		private var _treeSelectedItem:Object; 
		public function set treeSelectedItem(item:Object):void 
		{ 
			_treeSelectedItem = item; 
			if (!_treeSelectedItem)  
			{ 
				selectedIndex = -1; 
				text = ""; 
			}
			else
			{
				text = _treeSelectedItem[labelField];
			}
			dispatchEvent(new ListEvent("treeChange")); 
		} 
		[Bindable(event="treeChange")] 
		public function get treeSelectedItem():Object 
		{ 
			return _treeSelectedItem; 
		}
		
		/**
		 * 选中项的数组(通过CheckBox选择的项目) 
		 */		
		private var _treeSelectedItems:Array = [];
		public function set treeSelectedItems(array:Array):void
		{
			if (array)
			{
				_treeSelectedItems = array;
				dispatchEvent(new ListEvent("treeChange")); 
			}
		}
		public function get treeSelectedItems():Array
		{
			return _treeSelectedItems;
		}
		
		public function TreeComboBox() 
		{ 
			super(); 
			
			this.dropdownFactory = ddFactory; 
			
			this.addEventListener(DropdownEvent.OPEN, onComboOpen); 
			
			selectedIndex = -1; 
		} 
		
		/**
		 * 展开下拉树 
		 */		
		private function onComboOpen(event:DropdownEvent):void 
		{ 
			var tree:TreeComboBoxRenderer = dropdown as TreeComboBoxRenderer; 
			
			if (!isParentSelectable && treeSelectedItem is XML 
				&& (treeSelectedItem as XML).children().length())
			{
				treeSelectedItem = null;
			}
			else if (treeSelectedItem) 
			{ 
				tree.expandParents(treeSelectedItem); 
				tree.selectNode(treeSelectedItem); 
			} 
			else 
			{ 
				try{ 
					if (dataProvider[0]) 
					{ 
						tree.expandItem(dataProvider.getItemAt(0), true); 
					} 
				}catch(e:Error) 
				{ 
				} 
			} 
		} 
		
		/** 
		 * private
		 */ 
		override protected function updateDisplayList(unscaledWidth:Number,  
			unscaledHeight:Number):void  
		{  
			super.updateDisplayList(unscaledWidth, unscaledHeight);    
			
			if(dropdown && treeSelectedItem && treeSelectedItem[labelField] != null) 
			{    
				text = treeSelectedItem[labelField];  
			} 
		}  
		
		/**
		 * 是否可选
		 */		
		public function isSelectable(data:Object):Boolean 
		{ 
			return (_isParentSelectable || (data.children().length() <= 0)); 
		} 
		
		/**
		 * 更新选中项 
		 */		
		public function updateLabel(selectedItem:Object):void 
		{ 
			if (selectedItem) 
			{ 
				treeSelectedItem = selectedItem; 
			} 
		} 
	} 
} 